using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Net;
using System.Web.ApplicationServices;
using System.Web.Script.Serialization;

namespace Salient.Web.ApplicationServices
{
    ///<summary>
    ///</summary>
    public static class ProfileServiceHelper
    {
        ///<summary>
        ///</summary>
        public const string EndpointName = "Profile_JSON_AppService.axd";

        ///<summary>
        ///</summary>
        ///<param name="rootUrl"></param>
        ///<param name="cookies"></param>
        ///<param name="authenticatedUserOnly"></param>
        ///<returns></returns>
        public static Dictionary<string, object> ProfileGetAllPropertiesForCurrentUserAppSvc(string rootUrl,
                                                                                             CookieContainer cookies,
                                                                                             bool authenticatedUserOnly)
        {
            string response = RequestBuilder
                .PostJsonApp(rootUrl + EndpointName + "/GetAllPropertiesForCurrentUser",
                             new { authenticatedUserOnly }, cookies)
                .GetResponse().GetResponseStream().Text();
            return new JavaScriptSerializer().CleanAndDeserialize<Dictionary<string, object>>(response);
        }


        ///<summary>
        ///</summary>
        ///<param name="rootUrl"></param>
        ///<param name="cookies"></param>
        ///<param name="properties"></param>
        ///<param name="authenticatedUserOnly"></param>
        ///<returns></returns>
        public static Dictionary<string, object> ProfileGetPropertiesForCurrentUserAppSvc(string rootUrl,
                                                                                          CookieContainer cookies,
                                                                                          IEnumerable<string> properties,
                                                                                          bool authenticatedUserOnly)
        {
            string response = RequestBuilder
                .PostJsonApp(rootUrl + EndpointName + "/GetPropertiesForCurrentUser",
                             new { authenticatedUserOnly }, cookies)
                .GetResponse().GetResponseStream().Text();
            return new JavaScriptSerializer().CleanAndDeserialize<Dictionary<string, object>>(response);
        }

        ///<summary>
        ///</summary>
        ///<param name="rootUrl"></param>
        ///<param name="cookies"></param>
        ///<returns></returns>
        public static Collection<ProfilePropertyMetadata> ProfileGetPropertiesMetadataAppSvc(string rootUrl,
                                                                                             CookieContainer cookies)
        {
            string response = RequestBuilder
                .PostJsonApp(rootUrl + EndpointName + "/GetPropertiesMetadata", null, cookies)
                .GetResponse().GetResponseStream().Text();

            return new JavaScriptSerializer().CleanAndDeserialize<Collection<ProfilePropertyMetadata>>(response);
        }

        ///<summary>
        ///</summary>
        ///<param name="rootUrl"></param>
        ///<param name="cookies"></param>
        ///<param name="values"></param>
        ///<param name="authenticatedUserOnly"></param>
        ///<returns></returns>
        public static Collection<string> ProfileSetPropertiesForCurrentUserAppSvc(string rootUrl, CookieContainer cookies,
                                                                                  IDictionary<string, object> values,
                                                                                  bool authenticatedUserOnly)
        {
            string response = RequestBuilder
                .PostJsonApp(rootUrl + EndpointName + "/SetPropertiesForCurrentUser",
                             new { values, authenticatedUserOnly }, cookies).GetResponse().GetResponseStream().Text();
            return new JavaScriptSerializer().CleanAndDeserialize<Collection<string>>(response);
        }
    }
}